package array;

public class leetCode440_findKthNumber {

    public static void main(String[] args) {
        int n = 13;
        int k = 3;
        leetCode440_findKthNumber kk = new leetCode440_findKthNumber();
        kk.findKthNumber(n,k);
    }

    public int findKthNumber(int n, int k) {
        int cur = 1;
        k-=1;
        while (k > 0){
            int num = getNode(n,cur,cur + 1);
            if(num <= k){
                cur+=1;
                k-=num;
            }else{
                cur *= 10;
                k -= 1;
            }
        }
        return cur;

    }

    public int getNode(int n,long first,long last){
        int num = 0;
        while (first <= n){
            num += Math.min(last,n + 1) - first;
            first *= 10;
            last *= 10;
        }
        return num;
    }


}
